﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace _15_Iterator
{
    class Program
    {
        /// <summary>
        /// 迭代器模式
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //迭代器模式是一种行为设计模式， 让你能在不暴露集合底层表现形式 （列表、 栈和树等） 的情况下遍历集合中所有的元素。
            var collection = new WordsCollection();
            collection.AddItem("First");
            collection.AddItem("Second");
            collection.AddItem("Third");
            Console.WriteLine("Straight traversal:");

            foreach (var elment in collection)
            {
                Console.WriteLine(elment);
            }
            Console.WriteLine("\nReverse traversal:");

            collection.ReverseDirection();

            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
            Console.ReadKey();
        }
    }
    abstract class Iterator : IEnumerator
    {
        object IEnumerator.Current => Current();
        public abstract int Key();
        public abstract object Current();
        public abstract bool MoveNext();
        public abstract void Reset();
    }
    abstract class IteratorAggregate : IEnumerable
    {
        public abstract IEnumerator GetEnumerator();
    }
    class AlphabeticalOrderIterator : Iterator
    {
        private WordsCollection _collection;
        private int _position = -1;
        private bool _reverse = false;
        public AlphabeticalOrderIterator(WordsCollection collection,bool reverse=false)
        {
            _collection = collection;
            _reverse = reverse;
            if (reverse)
            {
                _position = collection.GetItems().Count;
            }
        }
        public override object Current()
        {
            return _collection.GetItems()[_position];
        }
        public override int Key()
        {
            return _position;
        }
        public override bool MoveNext()
        {
            int updatedPosition = _position + (_reverse ? -1 : 1);
            if(updatedPosition>=0 && updatedPosition < _collection.GetItems().Count)
            {
                _position = updatedPosition;
                return true;
            }
            else
            {
                return false;
            }
        }
        public override void Reset()
        {
            _position = _reverse ? _collection.GetItems().Count - 1 : 0;
        }
    }
    class WordsCollection : IteratorAggregate
    {
        List<string> _collection = new List<string>();
        bool _direction = false;
        public void ReverseDirection()
        {
            _direction = !_direction;
        }
        public List<string> GetItems()
        {
            return _collection;
        }
        public void AddItem(string item)
        {
            _collection.Add(item);
        }
        public override IEnumerator GetEnumerator()
        {
            return new AlphabeticalOrderIterator(this, _direction);
        }
    }
}
